﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using System.Timers;
using Reborn_WorldServer;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_IllusionFire : AreaSpell
    {
        public Spell_IllusionFire() { }

        override public bool isDangerous(Creature creature)
        {
            if (creature.IgnoresFire == true || creature.Smart == true)
                return false;
            
            return true;
        }

        private void MainCast(CreatureBase myCreatureBase, int MapX, int MapY, List<MapItem> MapItemsWithin12)
        {
            //Set Duration & Damage Per Character Stats
            DurationLow = 20;
            DurationHigh = 25;

            CastMapX = MapX;
            CastMapY = MapY;

            List<Character> CharactersWithinUpdateRange = new List<Character>();
            foreach (MapItem mapitem in MapItemsWithin12)
            {
                //Add this spell to each mapitem in range & record mapitem for timer
                if ((mapitem.MapX == CastMapX && mapitem.MapY == CastMapY) &&
                    mapitem.Motion > 0 && mapitem.TerrainType != "Water" && mapitem.Transparent == true)
                {
                    foreach (Spell spell in mapitem.SpellLayer0)
                    {
                        if (spell.spellIcon.Name == "Illusion Fire")
                        {
                            mapitem.SpellLayer0.Remove(spell);
                            break;
                        }
                    }
                    mapitem.SpellLayer0.Add(this);
                    MapItems.Add(mapitem);
                }
                if (Math.Abs(mapitem.MapX - CastMapX) <= 8 && Math.Abs(mapitem.MapY - CastMapY) <= 8)
                {
                    foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                    {
                        if (creaturebase is Character)
                        {
                            Character character = (Character)creaturebase;
                            CharactersWithinUpdateRange.Add(character);
                            character.PlaySound("FireBall", CastMapX, CastMapY);
                        }
                    }
                }
            }
            foreach (Character character in CharactersWithinUpdateRange)
            {
                character.AddMapItems(MapItems, false);
            }

            //Start Spell Timer
            spellTimer.Start();
        }
        //public void CreatureCast(CreatureBase myCreatureBase, int MapX, int MapY)
        //{
        //    if (base.InitializeCreatureCast(myCreatureBase) == false)
        //        return;

        //    //MainCast(myCreatureBase, MapX, MapY);
        //}

        public void Cast(CreatureBase myCreatureBase, int CastLevel, List<string> MoveList)
        {
            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, MoveList, out MapItemsWithin12) == false)
                return;

            MainCast(myCreatureBase, CastMapX, CastMapY, MapItemsWithin12);
        }

        protected override void SpellTimer_Expired(object source, ElapsedEventArgs e)
        {
            TimerCounter = TimerCounter + 1;

            //Get MapLock
            LockItem MapLock = Managers.GameHandler.GetLockItem(MapID, Instance);

            //Lock MapLock
            lock (MapLock.LockObj)
            {
                //Stop Running if no mapitems
                bool FoundItem = false;
                foreach (MapItem mapitem in MapItems)
                {
                    foreach (Spell spell in mapitem.SpellLayer0)
                    {
                        if (spell.SpellID == this.SpellID)
                        {
                            FoundItem = true;
                            break;
                        }
                    }
                }
                if (FoundItem == false)
                    return;

                List<MapItem> MapItemsToUpdate = new List<MapItem>();

                foreach (MapItem mapitem in MapItems)
                {
                    bool foundSpell = false;
                    foreach (Spell spell in mapitem.SpellLayer0)
                    {
                        if (spell.SpellID == this.SpellID)
                        {
                            foundSpell = true;
                            break;
                        }
                    }
                    if (foundSpell == false)
                        continue;

                    if (TimerCounter >= DurationLow)
                    {
                        if (mapitem.MapX != CastMapX || mapitem.MapY != CastMapY)
                        {
                            int RemoveSpell = Managers.GameHandler.random.Next(-1, DurationHigh - TimerCounter);
                            if (RemoveSpell == -1)
                            {
                                mapitem.SpellLayer0.Remove(this);
                                MapItemsToUpdate.Add(mapitem);
                                continue;
                            }
                        }
                        else if (TimerCounter >= DurationHigh)
                        {
                            mapitem.SpellLayer0.Remove(this);
                            MapItemsToUpdate.Add(mapitem);
                            continue;
                        }
                    }
                }

                //Get Map
                Map CurrentMap = Managers.GameHandler.GetMap(MapID, Instance);
                List<Character> CharsToUpdate = new List<Character>();
                //Send Updated Hexes to all characters within 8 of any hex effected
                foreach (MapItem mapitem in CurrentMap.MapItems)
                {
                    foreach (MapItem mitem in MapItemsToUpdate)
                    {
                        if (mapitem.MapX - mitem.MapX >= -4 && mapitem.MapX - mitem.MapX <= 4 && mapitem.MapY - mitem.MapY >= -4 && mapitem.MapY - mitem.MapY <= 4)
                        {
                            foreach (CreatureBase creatureBase in mapitem.CreaturesBases)
                            {
                                if (creatureBase is Character)
                                    CharsToUpdate.Add((Character)creatureBase);
                            }
                            break;
                        }
                    }
                }
                foreach (Character character in CharsToUpdate)
                {
                    character.AddMapItems(MapItemsToUpdate, false);
                }
            }
            spellTimer.Start();
        }

    }
}
